package cn.ctvonline.android.kaopu.widget;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.view.WindowManager;

import cn.ctvonline.android.kaopu.R;

/**
 * Created by dujian on 15/12/2.
 */
public class ChooserDialog extends Dialog {
    private RecyclerView valueRv;
    private RecyclerView.Adapter valueAdapter;
    private Context context;

    public ChooserDialog(Context context) {
        this(context, R.style.ChooserDialogStyle);
        this.context = context;
    }

    public ChooserDialog(Context context, int themeResId) {
        super(context, themeResId);
        this.context = context;
    }

    public ChooserDialog(Context context, RecyclerView.Adapter adapter) {
        this(context);
        this.context = context;
        this.valueAdapter = adapter;
    }

    private void init() {
        WindowManager m = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm = new DisplayMetrics();
        m.getDefaultDisplay().getMetrics(dm);
        float density = dm.density;
        int screenWidth = (int) (dm.widthPixels * density + 0.5f);      // 屏幕宽（px，如：480px）
        int screenHeight = (int) (dm.heightPixels * density + 0.5f);     // 屏幕高（px，如：800px）
        android.view.WindowManager.LayoutParams p = getWindow().getAttributes();  //获取对话框当前的参数值
        p.height = (int) (screenHeight * 0.6);
        p.width = screenWidth;    //宽度设置为全屏
        p.y = 200;
        getWindow().setLayout(screenWidth, (int) (screenHeight * 0.6));
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_value_chooser);
        valueRv = (RecyclerView) findViewById(R.id.dialog_value_rv);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
        valueRv.setLayoutManager(linearLayoutManager);
        valueRv.setAdapter(valueAdapter);
        init();
    }
}
